//
//  LeftMenu_ViewController.m
//  SlideMenu
//
//  Created by andli on 14-12-11.
//  Copyright (c) 2014年 Aryan Ghassemi. All rights reserved.
//

#import "LeftMenu_ViewController.h"

#import "LeftMenuItemModel.h"
#import "LeftMenuItemView.h"

#import "SlideNavigationContorllerAnimatorFade.h"
#import "SlideNavigationContorllerAnimatorSlide.h"
#import "SlideNavigationContorllerAnimatorScale.h"
#import "SlideNavigationContorllerAnimatorScaleAndFade.h"
#import "SlideNavigationContorllerAnimatorSlideAndFade.h"

@interface LeftMenu_ViewController ()<UIScrollViewDelegate>
{
    UIView *uvMainIcon;
    NSMutableArray *mainIconArray;
    UIScrollView *sv;
    NSArray *titleArray;
    NSArray *imageArray;
    
}
@property (nonatomic, assign) BOOL slideOutAnimationEnabled;
@end

@implementation LeftMenu_ViewController


#pragma mark - UIViewController Methods -

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self.slideOutAnimationEnabled = YES;
    
    return [super initWithCoder:aDecoder];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    sv = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 20+44, AppWidth, AppHeight-SystemTakeHeight-40)];
    sv.delegate = self;
    [self.view addSubview:sv];
    
    UIView *homeView = [[UIView alloc]initWithFrame:CGRectMake(0, sv.bottom, AppWidth, 1)];
    [homeView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"list_divider_line"]]];
    [self.view addSubview:homeView];
    
    UIButton *homeButton = [[UIButton alloc]initWithFrame:CGRectMake(-10, sv.bottom-5, 80, 40)];
    //    [homeButton setTitle:@"主页" forState:UIControlStateNormal];
    [homeButton setImage:[UIImage imageNamed:@"img_shhk_leftmenu_home.png"] forState:UIControlStateNormal];
    [homeButton addTarget:self action:@selector(clickHomeButton:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, homeButton.bottom-8, 40, 10)];
//    label.backgroundColor = [UIColor redColor];
    label.text = @"首页";
    label.textColor = [UIColor blackColor];
    label.font = [UIFont systemFontOfSize:10];
    [self.view addSubview:label];
    
    [self.view addSubview:homeButton];

    
     titleArray= [[NSArray alloc]initWithObjects:@"信息公开",@"门户新闻",@"部门动态",@"社区新闻",
                      @"公示公告",@"媒体报道",@"电子刊物",@"区情介绍",
                      @"图说门户",@"友好城区",@"门户年鉴",@"公共服务",
                      @"网上办事",@"办事服务",@"在线访谈",@"调查评议",
                      @"投资环境",@"政策指南",@"服务机构",@"申办指南",
                      @"投资服务",@"楼宇园区",@"企业上市",@"热点新闻",
                      nil];
     imageArray= [[NSArray alloc]initWithObjects:@"img_shhk_xwzx_info.png",@"img_shhk_xwzx_hkxw.png",@"img_shhk_xwzx_bmdt.png",@"img_shhk_xwzx_sqxw.png",
                           @"img_shhk_xwzx_gsgg.png",@"img_shhk_xwzx_mtbd.png",@"img_shhk_xwzx_dzkw.png",@"img_shhk_xwzx_qqjs.png",
                           @"img_shhk_xwzx_tshk.png",@"img_shhk_xwzx_yhcq.png",@"img_shhk_xwzx_hknj.png",@"img_shhk_xwzx_ggfw.png",
                           @"img_shhk_xwzx_wsbs.png",@"img_shhk_xwzx_bsfw.png",@"img_shhk_xwzx_zxft.png",@"img_shhk_xwzx_dcpy.png",
                           @"img_shhk_xwzx_tzhj.png",@"img_shhk_xwzx_zczn.png",@"img_shhk_xwzx_fwjg.png",@"img_shhk_xwzx_sbzn.png",
                           @"img_shhk_xwzx_tzfw.png",@"img_shhk_xwzx_lyyq.png",@"img_shhk_xwzx_qyss.png",@"img_shhk_xwzx_zjhk.png",
                           nil];

    
    mainIconArray = [[NSMutableArray alloc]init];
    for(int i=0;i< titleArray.count;i++){
        LeftMenuItemModel *info = [[LeftMenuItemModel alloc]init];
        info.modulename = [titleArray objectAtIndex:i];
        [mainIconArray addObject:info];
    }
    
    [self loadMainIconView];
}

-(void)loadMainIconView
{
    int MainIconPanelNums = mainIconArray.count%4==0?mainIconArray.count/4:mainIconArray.count/4+1;
    [uvMainIcon removeFromSuperview];
    uvMainIcon = [self getIconView:mainIconArray rect:CGRectMake(0, 0, AppWidth, MainItem_Width*MainIconPanelNums)];
    [sv addSubview:uvMainIcon];
    [sv setContentSize:CGSizeMake(AppWidth, uvMainIcon.frame.origin.y+uvMainIcon.frame.size.height)];
    
}

-(UIView *)getIconView:(NSMutableArray *)iArr rect:(CGRect)rect
{
    int MainIconPanelNums = iArr.count%4==0?MainIconPanelNums = iArr.count/4:iArr.count/4+1;
    
    UIView *MainIconView = [[UIView alloc]initWithFrame:rect];
    
    NSString* displayLine =  @"1";//显示线
    if ([displayLine isEqualToString:@"1"]) {
        for (int i=0; i<MainIconPanelNums; i++) {
            
            //加载边框
            UIView *uvPanel = [[UIView alloc]initWithFrame:CGRectMake(0, i*70, AppWidth, 70)];
            uvPanel.backgroundColor = [UIColor clearColor];
            
            UIImageView *ivLineTop = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, AppWidth, 1)];
            [ivLineTop setImage:[UIImage imageNamed:@"list_divider_line"]];
            [uvPanel addSubview:ivLineTop];
            
            UIImageView *ivLineBottom = [[UIImageView alloc]initWithFrame:CGRectMake(0, 70-1, AppWidth, 1)];
            [ivLineBottom setImage:[UIImage imageNamed:@"list_divider_line"]];
            [uvPanel addSubview:ivLineBottom];
            
            UIImageView *ivLine1 = [[UIImageView alloc]initWithFrame:CGRectMake(70, 0, 1, 70)];
            [ivLine1 setImage:[UIImage imageNamed:@"line_col"]];
            [uvPanel addSubview:ivLine1];
            
            UIImageView *ivLine2 = [[UIImageView alloc]initWithFrame:CGRectMake(140, 0, 1, 70)];
            [ivLine2 setImage:[UIImage imageNamed:@"line_col"]];
            [uvPanel addSubview:ivLine2];
            
            UIImageView *ivLine3 = [[UIImageView alloc]initWithFrame:CGRectMake(210, 0, 1, 70)];
            [ivLine3 setImage:[UIImage imageNamed:@"line_col"]];
            [uvPanel addSubview:ivLine3];
            
            if (i>0) {
                ivLineTop.hidden = YES;
            }
            [MainIconView addSubview:uvPanel];
        }
        
    }
    
    UIView *icoPanel = [[UIView alloc]initWithFrame:MainIconView.bounds];
    
    for (int i=0; i<iArr.count; i++) {
        
        LeftMenuItemModel *m = [iArr objectAtIndex:i];
        int icon_row = i/4;
        int icon_col = i%4;
        
        LeftMenuItemView *icon = [[LeftMenuItemView alloc]initWithFrame:CGRectMake(icon_col*70, 70*icon_row, 70,70) withHandler:^{
            [self clickItem:m];//点击
        }];
//        NSLog(@"(%i,%i,%i,%i)",icon_col*70, 70*icon_row, 70,70);
        icon.lbTitle.text = m.modulename;
        [icoPanel addSubview:icon];
        icon.ivIcon.image =[UIImage imageNamed:[imageArray objectAtIndex:i]];
    }
    [MainIconView addSubview:icoPanel];
    return MainIconView;
}

-(IBAction)clickHomeButton:(id)sender{
    //回到主界面
    [[SlideNavigationController sharedInstance] popToRootViewControllerAnimated:YES];
}

#pragma mark Item点击事件
-(void)clickItem:(LeftMenuItemModel*)m{
    
    NSLog(@"click %@",m.modulename);
    [[SlideNavigationController sharedInstance] popToRootAndSwitchToViewController:nil
                                                             withSlideOutAnimation:self.slideOutAnimationEnabled
                                                                     andCompletion:nil];
//    if ([m.modulename isEqualToString:@"调查评议"]) {
//        [self gotoXWZXView:m.modulename withCateNum:CateNum_DCPY];
//    }
//    if ([m.modulename isEqualToString:@"门户新闻"]) {
//        [self gotoXWZXView:m.modulename withCateNum:CateNum_HKNews];
//    }
//    if ([m.modulename isEqualToString:@"部门动态"]) {
//        [self gotoXWZXView:m.modulename withCateNum:CateNum_BMDT];
//    }
//    if ([m.modulename isEqualToString:@"社区新闻"]) {
//        [self gotoXWZXView:m.modulename withCateNum:CateNum_SQNews];
//    }
//    if ([m.modulename isEqualToString:@"公示公告"]) {
//        [self gotoXWZXView:m.modulename withCateNum:CateNum_GSGG];
//    }
//    if ([m.modulename isEqualToString:@"媒体报道"]) {
//        [self gotoXWZXView:m.modulename withCateNum:CateNum_MTBD];
//    }
//    if ([m.modulename isEqualToString:@"电子刊物"]) {
//        [self gotoXWZXView:m.modulename withCateNum:CateNum_DZKW];
//    }
//    
//    //走进门户：门户年鉴
//    if ([m.modulename isEqualToString:@"门户年鉴"]) {
//        [self gotoXWZXView:m.modulename withCateNum:CateNum_HKNJ];
//    }
//    if ([m.modulename isEqualToString:@"友好城区"]) {
//        [self gotoXWZXView:m.modulename withCateNum:CateNum_YHCQ];
//    }
//    
//    if ([m.modulename isEqualToString:@"图说门户"]) {
//        SHHK_TSHK_ViewController *view = [[SHHK_TSHK_ViewController alloc]init];
//        NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
//        view.passParam = params;
//        [[SlideNavigationController sharedInstance] popToRootAndSwitchToViewController:view
//                                                                 withSlideOutAnimation:self.slideOutAnimationEnabled
//                                                                         andCompletion:nil];
//    }
//    
//    if ([m.modulename isEqualToString:@"信息公开"]) {
//        SHHK_XXGK_ViewController *view = [[SHHK_XXGK_ViewController alloc]init];
//        NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
//        view.passParam = params;
//        [[SlideNavigationController sharedInstance] popToRootAndSwitchToViewController:view
//                                                                 withSlideOutAnimation:self.slideOutAnimationEnabled
//                                                                         andCompletion:nil];
//    }
//    
//    //投资环境，政策指南，服务机构，楼宇园区，企业上市
//    if ([m.modulename isEqualToString:@"投资环境"]) {
//        [self gotoZCZNView:m.modulename withCateNum:CateNum_TZHJ];
//    }
//    if ([m.modulename isEqualToString:@"政策指南"]) {
//        [self gotoZCZNView:m.modulename withCateNum:CateNum_ZCZN];
//    }
//    if ([m.modulename isEqualToString:@"服务机构"]) {
//        [self gotoZCZNView:m.modulename withCateNum:CateNum_FWJG];
//    }
//    
//    if ([m.modulename isEqualToString:@"楼宇园区"]) {
//        [self gotoZCZNView:m.modulename withCateNum:CateNum_LYYQ];
//    }
//    if ([m.modulename isEqualToString:@"企业上市"]) {
//        [self gotoZCZNView:m.modulename withCateNum:CateNum_QYSS];
//    }
//   
//    
//    if ([m.modulename isEqualToString:@"网上办事"]) {
//        SHHK_WSBS_ViewController *view = [[SHHK_WSBS_ViewController alloc]init];
//        NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
//        [params setObject:@"网上办事事项" forKey:@"title"];
//        view.passParam = params;
//        [[SlideNavigationController sharedInstance] popToRootAndSwitchToViewController:view
//                                                                 withSlideOutAnimation:self.slideOutAnimationEnabled
//                                                                         andCompletion:nil];
//    }
//    if ([m.modulename isEqualToString:@"办事服务"]) {
//        SHHK_BSFW_ViewController *view = [[SHHK_BSFW_ViewController alloc]init];
//        NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
//        [params setObject:@"办事服务部门" forKey:@"title"];
//        view.passParam = params;
//        [[SlideNavigationController sharedInstance] popToRootAndSwitchToViewController:view
//                                                                 withSlideOutAnimation:self.slideOutAnimationEnabled
//                                                                         andCompletion:nil];
//    }
//    if ([m.modulename isEqualToString:@"区情介绍"]) {
//        SHHK_WSBS_ViewController *view = [[SHHK_WSBS_ViewController alloc]init];
//        NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
//        [params setObject:@"区情介绍" forKey:@"title"];
//        view.passParam = params;
//        [[SlideNavigationController sharedInstance] popToRootAndSwitchToViewController:view
//                                                                 withSlideOutAnimation:self.slideOutAnimationEnabled
//                                                                         andCompletion:nil];
//    }
//    if ([m.modulename isEqualToString:@"公共服务"]) {
//        SHHK_WSBS_ViewController *view = [[SHHK_WSBS_ViewController alloc]init];
//        NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
//        [params setObject:@"公共服务" forKey:@"title"];
//        view.passParam = params;
//        [[SlideNavigationController sharedInstance] popToRootAndSwitchToViewController:view
//                                                                 withSlideOutAnimation:self.slideOutAnimationEnabled
//                                                                         andCompletion:nil];
//    }
//    if ([m.modulename isEqualToString:@"投资服务"]) {
//        SHHK_TZFW_ViewController *view = [[SHHK_TZFW_ViewController alloc]init];
//        NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
//        [params setObject:@"投资服务" forKey:@"title"];
//        [params setObject:CateNum_ZSYZ forKey:@"categoryid"];
//        [params setObject:@"" forKey:@"type"];
//        [params setObject:@"" forKey:@"ouguid"];
//        view.passParam = params;
//        [[SlideNavigationController sharedInstance] popToRootAndSwitchToViewController:view
//                                                                 withSlideOutAnimation:self.slideOutAnimationEnabled
//                                                                         andCompletion:nil];
//    }
//    if ([m.modulename isEqualToString:@"张江门户园"]) {
//        SHHK_ZJHKY_ViewController *view = [[SHHK_ZJHKY_ViewController alloc]init];
//        NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
//        [params setObject:@"张江门户园" forKey:@"title"];
//        view.passParam = params;
//        [[SlideNavigationController sharedInstance] popToRootAndSwitchToViewController:view
//                                                                 withSlideOutAnimation:self.slideOutAnimationEnabled
//                                                                         andCompletion:nil];
//    }
//    if ([m.modulename isEqualToString:@"申办指南"]) {
//        SHHK_SBZN_ViewController *view = [[SHHK_SBZN_ViewController alloc]init];
//        NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
//        [params setObject:@"申办指南" forKey:@"title"];
//        view.passParam = params;
//        [[SlideNavigationController sharedInstance] popToRootAndSwitchToViewController:view
//                                                                 withSlideOutAnimation:self.slideOutAnimationEnabled
//                                                                         andCompletion:nil];
//    }
//    
//    if ([m.modulename isEqualToString:@"在线访谈"]) {
//        SHHK_ZXFT_ViewController *view = [[SHHK_ZXFT_ViewController alloc]init];
//        NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
//        [params setObject:@"在线访谈" forKey:@"title"];
//        view.passParam = params;
//        [[SlideNavigationController sharedInstance] popToRootAndSwitchToViewController:view
//                                                                 withSlideOutAnimation:self.slideOutAnimationEnabled
//                                                                         andCompletion:nil];
//    }
}




//#pragma mark 新闻中心:门户新闻，部门动态，社区新闻，公示公告，媒体报道，电子刊物
//-(void)gotoXWZXView:(NSString *)title withCateNum:(NSString *)catenum{
//    SHHK_XWZX_ViewController *view = [[SHHK_XWZX_ViewController alloc]init];
//    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
//    [params setObject:title forKey:@"title"];
//    [params setObject:catenum forKey:@"catenum"];
//    view.passParam = params;
//    //    [self.navigationController pushViewController:view animated:YES];
//    [[SlideNavigationController sharedInstance] popToRootAndSwitchToViewController:view
//                                                             withSlideOutAnimation:self.slideOutAnimationEnabled
//                                                                     andCompletion:nil];
//}
//
//-(void)gotoZCZNView:(NSString *)title withCateNum:(NSString *)catenum{
//    SHHK_ZCZNList_ViewController *view = [[SHHK_ZCZNList_ViewController alloc]init];
//    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
//    [params setObject:title forKey:@"title"];
//    [params setObject:catenum forKey:@"catenum"];
//    view.passParam = params;
//    //    [self.navigationController pushViewController:view animated:YES];
//    [[SlideNavigationController sharedInstance] popToRootAndSwitchToViewController:view
//                                                             withSlideOutAnimation:self.slideOutAnimationEnabled
//                                                                     andCompletion:nil];
//}


@end
